package cn.com.jslh.boarpro.main.presenter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimAdapterEx;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.main.contract.ShowPigInfoContract;
import cn.com.jslh.boarpro.main.model.ShowPigInfoModelImpl;
import cn.com.jslh.boarpro.main.view.fragment.ShowPigInfoFragment;
import cn.com.jslh.boarpro.util.JSONUtil;
import cn.com.jslh.boarpro.util.WebService;
import cn.com.jslh.boarpro.util.template.TemplateHelper;

/**
 * Created by Administrator on 2017/11/23.
 */

public class ShowPigInfoPresenterImpl implements ShowPigInfoContract.Presenter {
    private ShowPigInfoContract.View view;
    private ShowPigInfoContract.Model model;
    private Context context;
    private SlimAdapterEx infoAdapter;

    public ShowPigInfoPresenterImpl(Context context, ShowPigInfoContract.View view) {
        view.setPresenter(this);
        this.view = view;
        this.context = context;
        model = new ShowPigInfoModelImpl();
    }

    @Override
    public void start() {

    }

    @Override
    public void bindRV(RecyclerView recyclerView) {
        infoAdapter = SlimAdapter.create(SlimAdapterEx.class)
                .enableDiff()
                .registerDefault(R.layout.add_breeding_info_list_item, new SlimInjector<JSONObject>() {
                    @Override
                    public void onInject(JSONObject object, IViewInjector injector) {
                        try {
                            injector.text(R.id.title, object.getString("data"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }).attachTo(recyclerView);
    }

    @Override
    public void getPigInfo(JSONArray jsonArray) {
        infoAdapter.updateData(JSONUtil.JsonArray2List(jsonArray)).notifyDataSetChanged();
    }

    @Override
    public void refresh() {
        infoAdapter.updateData(new ArrayList<Object>()).notifyDataSetChanged();
    }
}
